<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>Client Side Pagination in DataGrid - jQuery EasyUI Demo</title>

</head>
<body>
	<table style="padding:10px">
		<tr>
			<td>名字：</td>
			<td colSpan="2"><input id="displayName" class="easyui-textbox" data-options="required:true" style="width: 310px" ></td>
		</tr>
		<tr>
			<td>邮箱：</td>
			<td><input id="prefix" class="easyui-textbox" data-options="required:true" style="width: 90px" ></td>
			<td>
				<input id="cc1" name="dept" value="Loading..." style="width: 216px">
			</td>
		</tr>
		<tr>
			<td>密码：</td>
			<td colSpan="2">
				<div id="userPwd" name="userPwd"></div>
				<!-- 
				<input id="userPwd" name="userPwd" class="easyui-textbox" data-options="required:true,value:'Mjj@1234',iconCls:'icon-reload'" style="width: 310px"> 
				 -->
			</td>
		</tr>
		<tr>
			<td>订阅：</td>
			<td colSpan="2" id="licenseTd">
				<div th:each="list : ${session.licenseVo}" style="font-size: 12px">
					<span th:if="${list.capabilityStatus} eq 'Enabled'">
						<input class="easyui-checkbox" name="checkbox1" th:value="${list.skuId}" th:label="${list.skuIdDesc}" labelWidth="290px" ><br>
					</span>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="3" align="right">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">确认</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">取消</a>
			</td>
		</tr>
	</table>
	<script type="text/javascript" th:inline="none">
		var defaultPwd = "Mjj@1234";
		$.ajax({
		    type : "GET", 
		    url : "/getDefaultPwd",
		    async : false ,
		    success : function(result) {
		    	defaultPwd = result;
			}
		});
	
		$('#cc1').combobox({
		    url:'/getDomains',
		    valueField:'id',
		    textField:'text',
		    editable: false
		});
	
		$('#userPwd').textbox({
			required: true,
			width: 310,
			value: defaultPwd,
			icons:[{
				iconCls:'icon-reload',
				handler: function(e){
					passwordGen();
				}
			}]
		});
		
		function submitForm(){
			var mailNickname = $('#prefix').val();
			var userPrincipalName = $('#prefix').val() +  $('#cc1').combobox('getText');
			var displayName = $('#displayName').val();
			var userPwd = $('#userPwd').textbox('getText');
			if(mailNickname==''||displayName==''){
		      	$.messager.show({
					title:'错误',
					msg: '参数不足',
					timeout:2000,
					showType:'slide'
				});
		      	$('#dlg3').dialog('close');
				return;
			}
			
			var licenses = "";
			$('input:checkbox[name=checkbox1]:checked').each(function(i){
				if(0==i){
					licenses = $(this).val();
				}else{
					licenses += (","+$(this).val());
				}
			});
			if(licenses==null){
				console.log('未分配订阅');
			}
			else{
				console.log('分配订阅为 '+licenses);
			}
			
			$.ajax({
			    type : "POST", 
			    url : "/createOfficeUser",
			    data : { "mailNickname": mailNickname, "userPrincipalName":userPrincipalName , "displayName": displayName, "licenses": licenses, 'userPwd': userPwd },
			    success : function(result) {
			      	$.messager.show({
						title:'结果',
						msg: result,
						timeout:2000,
						showType:'slide'
					});
			      	$('#tb3').datagrid('reload');
				},
				error : function(){
			       	$.messager.show({
						title:'失败',
						msg:'创建用户失败',
						timeout:2000,
						showType:'slide'
					}); 
				}
			});
			
			$('#dlg3').dialog('close');
		}
		function clearForm(){
			$('#dlg3').dialog('close');
		}
		
		function passwordGen() {
		    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		    var string_length = 8;
		    var randomstring = '';
		    var charCount = 0;
		    var numCount = 0;

		    for (var i = 0; i < string_length; i++) {
		        // If random bit is 0, there are less than 3 digits already saved, and there are not already 5 characters saved, generate a numeric value. 
		        if ((Math.floor(Math.random() * 2) == 0) && numCount < 3 || charCount >= 5) {
		            var rnum = Math.floor(Math.random() * 10);
		            randomstring += rnum;
		            numCount += 1;
		        } else {
		            // If any of the above criteria fail, go ahead and generate an alpha character from the chars string
		            var rnum = Math.floor(Math.random() * chars.length);
		            randomstring += chars.substring(rnum, rnum + 1);
		            charCount += 1;
		        }
		    }
		    $('#userPwd').textbox('setText', randomstring);
		}
	</script>
	
</body>
</html>
